//
//  ChangeCityViewController.m
//  PrayerTimes
//
//  Created by Igor Karpenko on 6/8/13.
//  Copyright (c) 2013 Igor Karpenko. All rights reserved.
//

#import "ChangeCityViewController.h"
#import "Utils.h"

@interface ChangeCityViewController ()

@end

@implementation ChangeCityViewController

#pragma mark - Instance initialization

-(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (!self) {
        return  nil;
    }
	timesManager = [PrayerTimesManager sharedInstance];
	dataProvider = [DataProvider sharedInstance];
	
	return self;
}


#pragma mark - Memory managment

-(void) dealloc {
    [_countryTextField release];
    [_cityTextField release];
    [_saveButton release];
    [_searchButton release];
    [_cancelButton release];
	[_overlayImage release];
	
    [super dealloc];
}


#pragma mark - Interface methods

-(void) viewDidLoad {
    [super viewDidLoad];

	_overlayImage.image = [_overlayImage.image resizableImageWithCapInsets:UIEdgeInsetsMake(230.0f, 0.0f, 20.0f, 0.0f)];
	_saveButton.enabled = NO;
	
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didFinishTimeRequest:)
												 name:PrayerTimesManagerDidFinishTimeRequest
											   object:nil];
}


-(void) viewDidUnload {
	[[NSNotificationCenter defaultCenter] removeObserver:self];
	
    [self setCountryTextField:nil];
    [self setCityTextField:nil];
    [self setSaveButton:nil];
    [self setSearchButton:nil];
    [self setCancelButton:nil];
	[self setOverlayImage:nil];
    [super viewDidUnload];
}


-(IBAction) saveButtonTapped:(id)sender {
	[timesManager saveUserData];

	[self dismissViewControllerAnimated:YES completion:nil];
}


-(IBAction) searchButtonTapped:(id)sender {
	if (!_countryTextField.text.length || !_cityTextField.text.length) {
		[self showAlertWrongCityName];
		return;
	}
	
	[timesManager geocodeCounty:_countryTextField.text
						   city:_cityTextField.text];
}


-(IBAction) cancelButtonTapped:(id)sender {
	if ([DataProvider sharedInstance].currentCountry == nil) {
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Please enter city and counrtry name and save data" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
		[alert show];
		[alert release];
		return;
	}
	
	
	[timesManager cancelUserData];
	
	[self dismissViewControllerAnimated:YES completion:nil];
}


#pragma mark - Private methods 

-(void) showAlertWrongCityName {
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
													message:@"Please enter correct city or counrtry name"
												   delegate:nil
										  cancelButtonTitle:@"OK"
										  otherButtonTitles:nil];
	
	[alert show];
	[alert release];
}


#pragma mark - Notification observers

-(void) didFinishTimeRequest:(NSNotification *) notification {
	if ([[notification.object objectForKey:@"status"] boolValue]) {
		_countryTextField.text = [notification.object objectForKey:@"country"];
	 	_cityTextField.text = [notification.object objectForKey:@"city"];
		_saveButton.enabled = YES;
	} else {
		// fail
		_saveButton.enabled = NO;
	 	[self showAlertWrongCityName];
	}
}


#pragma mark - Delegated methods

-(BOOL) textFieldShouldReturn:(UITextField *)textField {
	if (textField.tag == _countryTextField.tag) {
		[_cityTextField becomeFirstResponder];
	} else {
		[textField resignFirstResponder];
	}
	return YES;
}

@end
